﻿using System.Text;

namespace Jayden.Dll.Web.Scripts
{
	public class JsStringBuilder
	{
		private int m_TabCount = 0;
		private StringBuilder m_Builder;

		public JsStringBuilder(int tabCount, StringBuilder builder)
		{
			m_TabCount = tabCount;
			m_Builder = builder;
		}
		public JsStringBuilder() : this(0, new StringBuilder()) { }

		public JsStringBuilder AppendTab(int count)
		{
			for (int c = 0; c < count; c++)
				m_Builder.Append('\t');
			return this;
		}
		public JsStringBuilder Append(string js)
		{
			m_Builder.Append(js);
			return this;
		}
		public JsStringBuilder AppendString(string text) { return Append(JsHelper.ToJs(text)); }
		public JsStringBuilder AppendBool(bool value) { return Append(JsHelper.ToJs(value)); }
		public JsStringBuilder AppendNull() { return Append(JsHelper.Null); }
		public JsStringBuilder AppendUndefined() { return Append(JsHelper.Undefined); }
		public JsStringBuilder AppendLine()
		{
			m_Builder.AppendLine();
			if (m_TabCount != 0)
				AppendTab(m_TabCount);
			return this;
		}
		public JsStringBuilder Declare(string varName) { return Append("var " + varName); }
		public JsStringBuilder Assign() { return Append(" = "); }
		public JsStringBuilder OpenBracket(string bracket, bool indent)
		{
			Append(bracket);
			if (indent)
			{
				m_TabCount++;
				AppendLine();
			}
			return this;
		}
		public JsStringBuilder CloseBracket(string bracket, bool indent)
		{
			if (indent)
			{
				m_TabCount--;
				AppendLine();
			}
			Append(bracket);
			return this;
		}

		public override string ToString() { return m_Builder.ToString(); }
	}
}
